package base.web.actions.base.model;


import java.io.IOException;
import java.util.List;

import base.service.bo.base.model.IBoBaseModel;
import base.service.domain.exception.DaoException;
import base.service.domain.exception.NoSuchBeanException;
import base.web.actions.BaseAction;
import base.web.tools.BeansHelp;
import base.web.tools.WebUtils;
/**
* @Title: BaseModelAction.java
* @Package base.web.actions.base.model
* @Description: TODO
* @author YeQing  
* @date 2012-6-10 下午03:18:48
* @version V1.0
 */
public class BaseModelAction extends BaseAction{

    private IBoBaseModel boBaseModel;

    public BaseModelAction() {
        try {
            this.boBaseModel = (IBoBaseModel) BeansHelp.getBeanInstance("boBaseModel");
        } catch (NoSuchBeanException e) {
            log.error(e.getMessage());
        }
    }

    /**
     * IndexJsp
     * @return
     */
	public String index() {
		return getResult();
	}

    /**
     * AddForm JSP
     * @return
     */
    public String viewAdd() {
        return getResult();
    }

    public String viewEdit() {
        try {
            mapParam.clear();
            mapParam = WebUtils.getParameterMap();
            List list = boBaseModel.getListBaseModel(mapParam);
            if(list.size() > 0) {
                model.put("data", list.get(0));
            }
            System.out.println();
        } catch (DaoException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return getResult();
    }

    public String add() {
        try{
            mapParam.clear();
            mapParam = WebUtils.getParameterMap();
            boBaseModel.insertBaseModel(mapParam);
            this.msg = "Add Success";
            System.out.println();
        }catch (Exception e){
            this.code = -1;
            this.msg = e.getMessage();
            this.expMsg = getExceptionMessage(e);
            log.error(e.getMessage());
        }
        return getResult();
    }

    public String update() {
        try{
            mapParam.clear();
            mapParam = WebUtils.getParameterMap();
            boBaseModel.updateBaseModel(mapParam);
            this.msg = "Edit Success";
            System.out.println();
        }catch (Exception e){
            this.code = -1;
            this.msg = e.getMessage();
            this.expMsg = getExceptionMessage(e);
            log.error(e.getMessage());
        }
        return getResult();
    }

    /**
     * Test Method TreeJsp
     * @return
     * @throws IOException
     */
	public String LoadNodes() throws IOException {
		mapParam.clear();
		mapParam = WebUtils.getParameterMap();
		System.out.println("123123123");
		WebUtils.getResponse().getWriter().write("12312asdasd31");
		return getResult();
	}

    /**
     * Data List Grid
     * @return
     */
    public String dataGrid() {
        try {
            mapParam.clear();
            mapParam = WebUtils.getParameterMap();
            //paramMap Must have key
            // 'dataList' list
            // 'total' listTotal
            model.put("dataList", boBaseModel.getListBaseModel(mapParam));
            model.put("total", boBaseModel.getListBaseModelCount(mapParam));
            msg = "Success";
        } catch (DaoException e) {
            this.code = -1;
			this.msg = e.getMessage();
			log.error(e.getMessage());
        }
        return "jsonGrid";
    }
	
}
